import java.util.PriorityQueue;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xwl
 */
public class PriorityQueue2 {

    /**
     * 返回堆中最大的k个数
     * @param arr:待输入堆中的数据
     * @param k:个数
     * @return
     */
    public int[] maxK(int[] arr, int k) {
        int[] ret = new int[k];
        if (arr == null || k == 0) {
            return ret;
        }
        Queue<Integer> minHeap = new PriorityQueue<>(k);
        //
        for (int i = 0; i < k; i++) {
            minHeap.offer(arr[i]);
        }
        //
        for (int i = k; i < arr.length; i++) {
            if (minHeap.peek() < arr[i]) {
                minHeap.poll();
                minHeap.offer(arr[i]);
            }
        }
        //
        for (int i = 0; i < k; i++) {
            ret[i] = minHeap.poll();
        }
        //
        return ret;
    }
}
